﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace HRdex_Final.Employee
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            //Panel1.Visible = true;
            //pOutput.Visible = false;
            Select();

        }

        protected void txtSubmit_Click(object sender, EventArgs e)
        {

            string EmployeeID = Request.QueryString["EmployeeID"];

            if (txtEmpNum.Text != EmployeeID)
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();

                conn.Open();

                SqlCommand cmd = new SqlCommand("Insert Into  formEmployees (EmployeeID, LastName, FirstName, MiddleName, catPositionsID, CollegeID, DeptID, UserLevelID, Password, Address, Birthday, Email, ContactNo)" +
                    "Values (@empNo, @lname, @fname, @mname, @position, @college, @dept, @ulevel, @pass, @address, @birthday, @email, @contactno)", conn);
                Stream fs = fuPicture.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);


                cmd.Parameters.Add("@empNo", SqlDbType.Int).Value = txtEmpNum.Text;
                cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = txtLName.Text;
                cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = txtFName.Text;
                cmd.Parameters.Add("@mname", SqlDbType.VarChar).Value = txtMName.Text;
                cmd.Parameters.Add("@position", SqlDbType.Int).Value = ddlPosition.Text;
                cmd.Parameters.Add("@college", SqlDbType.Int).Value = ddlCollege.Text;
                cmd.Parameters.Add("@dept", SqlDbType.Int).Value = ddlPosition.Text;
                cmd.Parameters.Add("@ulevel", SqlDbType.Int).Value = ddlUserLevel.Text;
                cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = txtPassword.Text;
                cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@birthday", SqlDbType.VarChar).Value = dpBirthday.SelectedDateString;
                cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
                cmd.Parameters.Add("@contactno", SqlDbType.VarChar).Value = txtContactNo.Text;
                //cmd.Parameters.Add("@pic", SqlDbType.Image).Value = bytes;


                cmd.ExecuteNonQuery();

                conn.Close();
            }
            else
            {
                Edit();
            }
        }

        protected void btnEdit_Click(object sender, ImageClickEventArgs e)
        {
            txtAddress.Enabled = true;
            txtCancel.Enabled = true;
            txtContactNo.Enabled = true;
            txtEmail.Enabled = true;
            txtEmpNum.Enabled = true;
            txtFName.Enabled = true;
            txtLName.Enabled = true;
            txtMName.Enabled = true;
            ddlCollege.Enabled = true;
            ddlDepartment.Enabled = true;
            ddlPosition.Enabled = true;
            ddlUserLevel.Enabled = true;
        }


        public void Select()
        {

            string EmployeeID = Request.QueryString["EmployeeID"];

            if (EmployeeID != null)
            {
                txtAddress.Enabled = false;
                txtCancel.Enabled = false;
                txtContactNo.Enabled = false;
                txtEmail.Enabled = false;
                txtEmpNum.Enabled = false;
                txtFName.Enabled = false;
                txtLName.Enabled = false;
                txtMName.Enabled = false;
                ddlCollege.Enabled = false;
                ddlDepartment.Enabled = false;
                ddlPosition.Enabled = false;
                ddlUserLevel.Enabled = false;

                SqlCommand cmd12 = new SqlCommand(" SELECT [EmployeeID], [LastName], [FirstName], [MiddleName], [catPositionsID], [CollegeID], [DeptID]," +
                "[UserLevelID], [Address], [Birthday], [ContactNo], [Email] FROM [formEmployees] WHERE ([EmployeeID] = " + EmployeeID + ")", conn);
                SqlDataAdapter sqlda = new SqlDataAdapter(cmd12);
                DataTable dt12 = new DataTable("dt12");
                sqlda.Fill(dt12);

                txtEmpNum.Text = dt12.Rows[0].ItemArray[0].ToString();
                txtLName.Text = dt12.Rows[0].ItemArray[1].ToString();
                txtFName.Text = dt12.Rows[0].ItemArray[2].ToString();
                txtMName.Text = dt12.Rows[0].ItemArray[3].ToString();
                ddlPosition.SelectedValue = dt12.Rows[0].ItemArray[4].ToString();
                ddlCollege.SelectedValue = dt12.Rows[0].ItemArray[5].ToString();
                ddlDepartment.SelectedValue = dt12.Rows[0].ItemArray[6].ToString();
                ddlUserLevel.SelectedValue = dt12.Rows[0].ItemArray[7].ToString();
                txtAddress.Text = dt12.Rows[0].ItemArray[8].ToString();
                dpBirthday.SelectedDateString = dt12.Rows[0].ItemArray[9].ToString();
                txtContactNo.Text = dt12.Rows[0].ItemArray[10].ToString();
                txtEmail.Text = dt12.Rows[0].ItemArray[11].ToString();
            }

        }
        public void Edit()
        {

            string EmployeeID = Request.QueryString["EmployeeID"];


            SqlCommand cmd7 = new SqlCommand("UPDATE formEmployees SET LastName = @lname, FirstName = @fname, MiddleName =@mname, " +
            "catPositionsID = @posID, CollegeID =@colID, DeptID =@deptID, UserLevelID =@userlevel, Address =@address, Birthday =@Birthday, " +
            "ContactNo =@ConNum, Email =@email, Password =@password WHERE(EmployeeID = " + EmployeeID + ")", conn);
            SqlDataAdapter da7 = new SqlDataAdapter(cmd7);
            conn.Open();

            cmd7.Parameters.AddWithValue("@lname", SqlDbType.VarChar).Value = txtLName.Text;
            cmd7.Parameters.AddWithValue("@fname", SqlDbType.VarChar).Value = txtFName.Text;
            cmd7.Parameters.AddWithValue("@mname", SqlDbType.VarChar).Value = txtMName.Text;
            cmd7.Parameters.AddWithValue("@posID", SqlDbType.Int).Value = ddlPosition.SelectedValue;
            cmd7.Parameters.AddWithValue("@colID", SqlDbType.Int).Value = ddlCollege.SelectedValue;
            cmd7.Parameters.AddWithValue("@deptID", SqlDbType.Int).Value = ddlDepartment.SelectedValue;
            cmd7.Parameters.AddWithValue("@userlevel", SqlDbType.Int).Value = ddlUserLevel.SelectedValue;
            cmd7.Parameters.AddWithValue("@address", SqlDbType.VarChar).Value = txtAddress.Text;
            cmd7.Parameters.AddWithValue("@Birthday", SqlDbType.VarChar).Value = dpBirthday.SelectedDateString;
            cmd7.Parameters.AddWithValue("@conNum", SqlDbType.Int).Value = txtContactNo.Text;
            cmd7.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = txtEmail.Text;
            cmd7.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = txtPassword.Text;

            cmd7.ExecuteNonQuery();
            conn.Close();


        }
    }
}